import {
	funcVerify,
} from '@utils/router'
import {
	userDelApi,
} from '@utils/apis'

let _submitUrl = ''

module.exports = {

	userAdd(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		comps.userName.textbox('readonly', false)
		comps.userForm.form('clear')
		comps.formDialog.dialog('open')
		_submitUrl = user.funcMap[func_id].func_url
	},

	userEdit(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		let data = comps.dataGrid.datagrid('getSelected')
		if (!data) {
			$.messager.alert('警告', '请选择！');
			return false
		}
		comps.userName.textbox('readonly', true)
		comps.userForm.form('load', data)
		comps.formDialog.dialog('open')
		_submitUrl = user.funcMap[func_id].func_url
	},

	userDel(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		let dataGrid = comps.dataGrid
		let data = dataGrid.datagrid('getSelected')
		if (!data) {
			$.messager.alert('警告', '请选择！', 'warning');
			return false
		}
		if (data.user_id == '00000000') {
			$.messager.alert('警告', '管理员不能删除！', 'error');
			return false
		}
		let user_id = data.user_id
		console.log('---data---', data)
		$.messager.confirm({
			title: '删除',
			msg: '确认删除？',
			fn: function(r) {
				if (r) {
					userDelApi({
						user_id,
					}).then(r => {
						dataGrid.datagrid('reload');
					})
				}
			}
		});
	},

	submit(comps, user) {
		$.messager.progress();
		let {
			userForm,
			dataGrid,
			formDialog,
		} = comps
		console.log('---_submitUrl---', _submitUrl)
		userForm.form('submit', {
			url: _submitUrl,
			onSubmit: function() {
				let isValid = userForm.form('validate');
				if (!isValid) {
					$.messager.progress('close');
				}
				_submitUrl = ''
				return isValid;
			},
			success: function(res) {
				console.log('---res---', res)
				try {
					res = JSON.parse(res)
				} catch (e) {

				}
				if (res.code != '000') {
					$.messager.alert(res.code, res.message)
				}
				$.messager.progress('close');
				dataGrid.datagrid('reload');
				formDialog.dialog('close');
				userForm.form('clear')
			}
		})
	},

	cancel(comps) {
		comps.userForm.form('clear')
		comps.formDialog.dialog('close')
	},

}